package user

import (
	"context"
	"time"

	"go-weixin/app/usercenter/api/internal/svc"
	"go-weixin/app/usercenter/api/internal/types"
	"go-weixin/app/usercenter/rpc/pb/proto/usercenter"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserDetailLogic {
	return &UserDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserDetailLogic) UserDetail(req *types.UserDetailReq) (resp *types.UserDetailResp, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	res, err := l.svcCtx.UserRpc.UserDetail(ctx, &usercenter.UserDetailReq{
		Id: req.Id,
	})
	if err != nil {
		return nil, err
	}
	var user types.User
	userInfo := res.UserInfo
	user.Id = userInfo.Id
	user.Name = userInfo.Name
	user.Activity = userInfo.Activity
	user.Country = userInfo.Country
	user.UpdateAt = userInfo.UpdateAt
	user.CreateAt = userInfo.CreateAt
	user.Gender = userInfo.Gender
	user.OpenId = userInfo.OpenId
	user.Phone = userInfo.Phone

	resp = new(types.UserDetailResp)
	resp.UserInfo = user
	return
}
